"""
Views for django_undo

"""

from django.views.generic.simple import redirect_to
from django.http import Http404

from utils import UndoOperationDoesNotExist, do_undo

def undo(request, undo_id, **kwargs):
    """
    Generic Undo View.
    Undo an operation and then redirect.
    Raises a 404 error if the undo_id doesn't match the currently saved undo operation.
    Redirects to the redirect_to value stored in the undo operation.

    TODO: only accept POST, else return 404
    """

    try:
        url = do_undo(request, undo_id)
    except UndoOperationDoesNotExist, details:
        raise Http404, details

    return redirect_to(request, url, **kwargs)
